package managementSystem;

import java.util.*;

@SuppressWarnings("unchecked")
public class Test {
    public static void main(String[] args){
        try{
            String in;
            String path = "C:\\Users\\梁俊华\\Desktop\\data.txt";
            Scanner scanner = new Scanner(System.in);
            FileOperate fileOperate = new FileOperate();
            ManagementSystem managementSystem = new ManagementSystem();
            String[] menu = {"新增学生", "删除学生", "修改学生", "查询学生", "学生列表", "统计", "保存", "退出"};
            ArrayList<HashMap<String, Object>> readerData = fileOperate.reader(path);
            if(readerData != null){
                System.out.println(managementSystem.initialization(readerData) ? "数据加载成功" : "数据加载失败");
            }
            while (true){
                try{
                    Object result;
                    for(int i = 0; i < menu.length; i++){
                        System.out.println((i + 1) + "." + menu[i]);
                    }
                    in = scanner.nextLine();
                    if(in.equals("8")){
                        break;
                    }
                    switch (in){
                        case "1":
                            result = managementSystem.create();
                            System.out.println(result != null ? ("新增成功：" + result) : "新增失败");
                            break;

                        case "2":
                            result = managementSystem.delete();
                            System.out.println(((Number)result).intValue() != 0 ? ("删除成功：" + result) : "删除失败");
                            break;

                        case "3":
                            result = managementSystem.update();
                            System.out.println(((Number)result).intValue() != 0 ? ("更新成功：" + result) : "更新失败");
                            break;

                        case "4":
                            result = managementSystem.get();
                            System.out.println(result != null ? ("查询成功：" + (HashMap<String, Object>)result) : "查询失败");
                            break;

                        case "5":
                            result = managementSystem.getList();
                            System.out.println(((ArrayList<HashMap<String, Object>>)((HashMap<String, Object>) result).get("data")).size() != 0 ? "学生列表：" : "查询失败");
                            for(HashMap<String, Object> data : (ArrayList<HashMap<String, Object>>)((HashMap<String, Object>) result).get("data")){
                                System.out.println(data);
                            }
                            break;

                        case "6":
                            managementSystem.statistics();
                            break;

                        case "7":
                            result = managementSystem.getList();
                            fileOperate.write(path, (ArrayList<HashMap<String, Object>>)((HashMap<String, Object>) result).get("data"));
                            break;

                        case "8":
                            break;

                        default:
                            System.out.println("选项不存在");
                            break;
                    }
                    System.out.println();
                }
                catch (Exception err){
                    System.out.println("errMsg:" + err.getMessage());
                }
            }
        }
        catch (Exception err){
            System.out.println("errMsg:" + err.getMessage());
        }
    }
}